//
//  FindViewController.m
//  MyProject
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//



//      主题美食页面
#import "FindViewController.h"
#import "WHZAfnetWorking.h"
#import "FindCollectionModel.h"


#import "GuestViewController.h"
#import "NetWork.h"

#define WIDTH 50
#define IMAGE_COUNT 5

@interface FindViewController ()

@end

@implementation FindViewController


//      视图将要出现
- (void)viewWillAppear:(BOOL)animated{
    
    [NetWork netWork:self];
    
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    
    
    //      创建一个 seg
    [self createSegment];
    self.navigationController.navigationBar.translucent = NO;

    //      创建背景 collectionView 的轮播图
    self.findBack = [[FindBackView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.findBack];
    self.findBack.backgroundColor = [UIColor grayColor];
    

    //      创建一个 中间 collectionView 的轮播图
    self.findCollectionView = [[FindCollectionView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 100 * 15, self.view.frame.size.width, self.view.frame.size.height / 100 * 45)];
    [self.view addSubview: self.findCollectionView];
    
    


    
    
    
    
    
    //      解析数据
    [WHZAfnetWorking GETJRAfnNetworkingWithUrl:@"http://food.boohee.com/fb/v1/topics/" WithParameters:nil Block:^(id result) {
        self.arrayFindViewController = [NSMutableArray array];
        self.dictionaryFindViewController = result;
        NSMutableArray *array = self.dictionaryFindViewController[@"topics"];
        
        for (NSMutableDictionary *tempDic in array) {
            FindCollectionModel *model = [[FindCollectionModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrayFindViewController addObject:model];
        }
        self.findCollectionView.arrayFindCollection = self.arrayFindViewController;
        self.findBack.arrayFindCollection = self.arrayFindViewController;
        [self.findCollectionView.findCollectionView reloadData];
        [self.findBack.findCollectionView reloadData];
    }];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(click:)];
}


- (void)click:(UIButton *)button{
    
    GuestViewController *guestVC = [[GuestViewController alloc] init];
    [self.navigationController pushViewController:guestVC animated:YES];
    
    
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    
//    if (scrollView == self.findBack.findCollectionView) {
//        
//        self.findBack.findCollectionView.contentOffset = CGPointMake(self.findCollectionView.findCollectionView.contentOffset.x * 10, 0);
//    }
//    
//}



- (void)createSegment{
    
    self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"主题美食", @"小知识", nil]];
    self.segment.frame = CGRectMake(self.view.frame.size.width / 5, 15, self.view.frame.size.width * 3 / 5, 30);
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0 / 255.0 green:147/255.0 blue:52/ 255.0 alpha:0.9];
    [self.segment addTarget:self action:@selector(changeAction) forControlEvents:(UIControlEventValueChanged)];
    self.navigationItem.titleView = self.segment;
    
}








- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

- (void)changeAction{
    //使用容器视图控制器切换页面
    
    NSInteger segIndex = [self.segment selectedSegmentIndex];
    
    if (segIndex == 0) {
        NSLog(@"segment 0");
        
        [self.view sendSubviewToBack:self.knowledgeVC.view];
        [self.knowledgeVC.view removeFromSuperview];
        
    }
    
    if (segIndex == 1) {
        
        NSLog(@"segment 1");
        
        self.knowledgeVC = [[SmallKonwledgeViewController alloc] init];
        //        self.knowledgeVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64);
        self.knowledgeVC.view.backgroundColor = [UIColor whiteColor];
        [self addChildViewController:self.knowledgeVC];
        [self.view addSubview:self.knowledgeVC.view];
        
        [self.view bringSubviewToFront:self.knowledgeVC.view];
        
    }
}




-(void)drawMyLayer{
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    
    //      获得根图层
    CALayer *layer = [[CALayer alloc]init];
    
    //      设置背景颜色,由于QuartzCore是跨平台框架，无法直接使用UIColor
    layer.backgroundColor = [UIColor colorWithRed:0 green:146 / 255.0 blue:1.0 alpha:1.0].CGColor;
    
    //      设置中心点
    layer.position = CGPointMake(size.width / 2, size.height / 2);
    
    //      设置大小
    layer.bounds = CGRectMake(0, 0, WIDTH,WIDTH);
    
    //      设置圆角,当圆角半径等于矩形的一半时看起来就是一个圆形
    layer.cornerRadius = WIDTH / 2;
    
    //      设置阴影
    layer.shadowColor = [UIColor grayColor].CGColor;
    layer.shadowOffset = CGSizeMake(2, 20);//阴影位置
    layer.shadowOpacity = .9;
    
    //      设置边框
    layer.borderColor = [UIColor whiteColor].CGColor;
//    layer.borderWidth = 1;
    
    //      设置锚点
    //        layer.anchorPoint = CGPointZero;
    
    [self.view.layer addSublayer:layer];
    
}



#pragma mark 点击放大

//      RootViewController 自带的方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    CALayer *layer = self.view.layer.sublayers[0];
    CGFloat width = layer.bounds.size.width;
    
    if (width == WIDTH) {
        width = WIDTH * 4;
    }else{
        width = WIDTH;
    }
    
    layer.bounds = CGRectMake(0, 0, width, width);
    layer.position = [touch locationInView:self.view];
    layer.cornerRadius =  self.view.frame.size.width / 100 * 70;
    
}










@end
